<?php
namespace app\admin\controller;

class Poster extends Admin{
    public function __construct(){
        parent::__construct();
        $this->model = "Poster";
        $this->assign("model",$this->model);
    }
    // public function index(){
    //     $getData = input('param.');
    //     $whereData = '';
    //     if ($this->uinfo['roleid'] != 1) {
    //         $whereData = 'userid='.$this->uinfo['userid'];
    //     }
    //     if ($getData) {
    //         foreach ($getData as $k=>$v) {
    //             if (!$whereData) {
    //                 $whereData .= $k.'='.$v;
    //             }else{
    //                 $whereData .= '&'.$k.'='.$v;
    //             }
    //         }
    //     }
    //     $this->assign('whereData', $whereData);
    //     $this->assign('keywords', isset($getData['keywords']) ? $getData['keywords'] : '');
    //     return $this->fetch();
    // }
    protected function get_where_map(){
        // $whereData = input("param.whereData/s");
        $keywords = input('param.keywords');
        // $whereData = urldecode($whereData);
        $whereDatas = [];
        // if ($whereData) {
        //     $whereData = explode('&', $whereData);
        //     foreach ($whereData as $v) {
        //         $r = explode('=', $v);
        //         if ($r[0] == 'keywords') {
        //             $whereDatas = [];
        //             $whereDatas['id|title|descrip'] = ['like', '%'.$r[1].'%'];
        //             break;
        //         }else{
        //             $whereDatas[$r[0]] = $r[1];
        //         }
        //     }
        // }
        if ($this->uinfo['roleid'] != 1) {
            $whereDatas['userid'] = $this->uinfo['userid'];
        }
        if ($keywords) {
            $whereDatas['id|title|descrip'] = ['like', "%$keywords%"];
        }
        return $whereDatas;
    }
    public function sel_dealer(){
        $id = input('param.id');
        $dealer_list = db('DealerContent')->where('contentid', $id)->where('typeid', 2)->select();
        $dealer_ids = [];
        foreach ($dealer_list as $v) {
            $dealer_ids[] = $v['dealer_id'];
        }
        $list = model('AdminUser')->list_data(['puserid'=>$this->uinfo['userid'], 'roleid'=>6]);
        $this->assign('list', $list);
        $this->assign('dealer_list', $dealer_ids);
        $this->assign('id', $id);
        return $this->fetch();
    }
    public function set_dealer(){
        $db = model($this->model);
        $postData = input("post.postData");
        parse_str($postData,$dataarr);
        // trace($dataarr['dealer_ids']);
        db('DealerContent')->where('contentid', $dataarr['id'])->where('typeid', 2)->delete();
        foreach ($dataarr['dealer_ids'] as $v) {
            $r = [
                'contentid' => $dataarr['id'],
                'typeid' => 2,
                'dealer_id' => $v,
                'roleid' => $this->uinfo['roleid']
            ];
            db('DealerContent')->insert($r);
        }
        // $dealer_ids = implode(',', $dataarr['dealer_ids']);
        // $ids = $db->where('id', $dataarr['id'])->setField('dealer_ids', $dealer_ids);
        // if($ids === false){
        //     return json(['status' => 0,'msg'=>$db->getError()]);
        // }
        return json(['status' => 1,'msg'=>'操作成功']);
    }
    public function data_save(){
        $db = model($this->model);
        $postData = input("post.postData");
        parse_str($postData,$dataarr);
        // $batch_adds = explode("\n", $dataarr['more_title']);
        // trace($batch_adds);exit();
        $dataarr['userid'] = $this->uinfo['userid'];
        $dataarr['roleid'] = $this->uinfo['roleid'];
        $id = $db->save_data($dataarr);
        if($id === false){
            return json(['status' => 0,'msg'=>$db->getError()]);
        }
        if ($this->uinfo['roleid'] == 6) {
            $r = [
                'contentid' => $id,
                'typeid' => 2,
                'dealer_id' => $this->uinfo['userid'],
                'roleid' => $this->uinfo['roleid']
            ];
            db('DealerContent')->insert($r);
        }
        return json(['status' => 1,'msg'=>'操作成功']);
    }
    // public function index(){
    //     $pid = input('param.pid', 0);
    //     $this->assign("pid",$pid);
    //     return $this->fetch();
    // }
    // protected function get_where_map(){
    //     $pid = input('param.pid', 0);
    //     $where['pid'] = $pid;
    //     return $where;
    // }
    // public function data_show(){
    //     $model = input("param.model");
    //     $pid = input("param.pid", 0);
    //     $page = input("param.page",1);
    //     $limit = input("param.limit",$this->default_page_items);
    //     $db = model($model);
    //     $offset = ($page - 1) * $limit;
    //     list($list,$where) = $db->get_list([],['pid'=>$pid],$limit,$offset);
    //     $count = $db->where('pid', $pid)->count();
    //     $return = [
    //         'code' => 0,
    //         'msg' => '',
    //         'count' => $count,
    //         'data' => $list,
    //     ];
    //     // trace($return);
    //     return json($return);
    // }
    // public function add(){
    //     $menus_list = model("Menu")->order("id")->select();
    //     $menus_list = json_decode(json_encode($menus_list),true);
    //     $Tree = new \base\tree\Tree;
    //     $menus = $Tree->toFormatTree($menus_list);
    //     $menus = array_merge([0 => ['id'=>0,'title_show'=>'顶级菜单']], $menus);
    //     $this->assign('Menus', $menus);
    //     return $this->fetch();
    // }
    // public function edit(){
    //     $items = input("param.items");
    //     $items = trim($items,",");
    //     $model = input("param.model");
    //     $db = model($model);
    //     $info = $db->get_data(['item_id' => $items]);
    //     $this->assign("info",$info);
        
    //     $menus_list = model("Menu")->order("id")->select();
    //     $menus_list = json_decode(json_encode($menus_list),true);
    //     $Tree = new \base\tree\Tree;
    //     $menus = $Tree->toFormatTree($menus_list);
    //     $menus = array_merge([0 => ['id'=>0,'title_show'=>'顶级菜单']], $menus);
    //     $this->assign('Menus', $menus);
    //     return $this->fetch();
    // }
}